import {getGoodsAPI, getCateAPI,addGoodAPI,getBrandListApi} from '../api/goodsAPI'
import Vue from 'vue'
let that = Vue.prototype
const goodsAbout = {
    namespaced: true,
    state:{
        total: 0,
        // 商品列表
        goodslist: [],
        // 品牌列表
        brandList: [],
        // 分类列表
        cateList: [],
    },
    actions: {
        // 获取全部商品
        async getGoodsData(context, query){
            let data = await getGoodsAPI(query)
            context.commit('get_goods_data', data)
        },
        // 获取品牌信息
        async getBrandData(context){
            let data = await getBrandListApi()
            context.commit('get_brand_data', data)
        },
        // 获取商品分类
        async getCateData(context){
            let data = await getCateAPI()
            context.commit('get_cate_data', data)
        },
        // 添加商品
        async addGoodData(context, query){
            let data = await addGoodAPI(query)
            context.commit('add_good_data', data)
        }
    },
    mutations: {
        // 获取全部商品映射getGoodsData
        get_goods_data(state, data){
            if(data.status !== 200) that.$message.error('获取商品列表失败!');
            state.goodslist = data.data.data
            state.total = data.data.total
        },
        // 获取品牌信息映射getBrandData
        get_brand_data(state, data){
            state.brandList = data.data.data
        },
        // 获取商品分类
        get_cate_data(state, data){
            state.cateList = data.data
        },
        // 添加商品映射addGoodData
        add_good_data(state, data){
            if(data.status!==200) return that.$message.error('添加商品失败！')
            that.$message.success('添加商品成功！')
        }
    }
  }

  export default goodsAbout